class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        return reverseKGroup(head, head, k, 1);
    }

    public ListNode reverseKGroup(ListNode head, ListNode tail, int k, int k0) {
        if (tail == null) {
            return head;
        }
        if (k0 % k == 0) {
            ListNode nex = reverseKGroup(tail.next, tail.next, k, k0 + 1);
            ListNode prev = head;
            ListNode now = head.next;
            while (prev != tail) {
                ListNode next = now.next;
                now.next = prev;
                prev = now;
                now = next;
            }
            head.next = nex;
            return tail;
        }
        return reverseKGroup(head, tail.next, k, k0 + 1);
    }
}